<?php
class StatSingleCategoryAction extends CAction {
	public function run() {
		$statModel = new AccountTokenSingleCategoryStat();
		$statModel->unsetAttributes(); // clear any default values
		$overallStat = array();
		if (isset($_GET['AccountTokenSingleCategoryStat'])) {
			$statModel->attributes = $_GET['AccountTokenSingleCategoryStat'];

			if ($statModel->validate()) {
				$itemModel = AccountTokenItem::model();
				$commandBuilder = Yii::app()->db->getSchema()->getCommandBuilder();
				$criteria = $statModel->createCriteria();
				$criteria->join = 'INNER JOIN {{account_token}} as token on tokenId=token.id INNER JOIN {{account_token_category}} as category on categoryId=category.id';

				$criteria->select = 'sum(debit) as totalDebit, sum(credit) as totalCredit';
				$command = $commandBuilder
					->createFindCommand($itemModel->getTableSchema(), $criteria);
				$row = $command->queryRow();
				//$diff = $row['totalDebit'] - $row['totalCredit'];
				$overallStat = array(
					//					'id' => 0,
					//					'name' => '总览',
					'totalDebit' => $row['totalDebit'],
					'totalCredit' => $row['totalCredit'],
				//'direction' => $diff > 0 ? '借' : ($diff < 0 ? '贷' : '平'),
				//'totalSum' => abs($diff),
				);
			}
		}
		$this->getController()
			->render('statSingleCategory',
				array(
					'statModel' => $statModel,
					'overallStat' => $overallStat,
				));
	}
}
